import { ref, computed } from "vue"

export async function usePage(initListApiCallback) {
    // 路由实例
    const route = useRoute();

    // 分页
    const page = ref(parseInt(route.params.page));

    // 总页
    const limit = ref(10);

    // 方法
    const { data, pending, error, refresh } = await initListApiCallback({
        page: page.value,
        limit: limit.value
    });



    // 数据
    const rows = computed(() => data.value?.rows ?? []);

    // 总数
    const total = computed(() => data.value?.count ?? 0);


    // 分页方法
    const handlePageChange = (e) => {
        navigateTo({
            params: {
                ...route.params,
                page: e
            },
            query: {
                ...route.query
            }
        })
    }


    return {
        page,
        limit,
        data,
        pending,
        error,
        refresh,
        rows,
        total,
        handlePageChange
    }
}